<?php

class LeftMenu {
	
	private $modules;
	private $current;
	
	public function __construct(){
		
		$this->findBackends();		
	}
	
	public function findBackends(){
		
		$d = dir("modules/");
		while (false !== ($entry = $d->read())) {
			
			if($entry == '.' || $entry == '..'){
				continue;
			}
			
			if(file_exists("modules/" . $entry . '/backend.php')){
				include_once("modules/" . $entry . '/backend.php');
				if(class_exists(ucfirst($entry) . "Backend")){
					
					$vars = get_class_vars(ucfirst($entry) . "Backend");					
					$modName = $vars["modName"];
				}
				else{
					$modName = $entry;
				}
				
				$this->modules[$entry] = array(
					'mod' => "modules/" . $entry . '/backend.php',
					'modName' => $modName,
					);
				
			}
		}
		$d->close();
	}
	
	public function getIncludePath($param) {
		return $this->modules[$param];
	}
	
	public function buildMenu(){
		
		$out = '';
		
		$tpl = file_get_contents(realpath(dirname(__FILE__) . "/..") . "/tpl/menu_item.tpl");
		$tpl_selected = file_get_contents(realpath(dirname(__FILE__) . "/..") . "/tpl/menu_item_selected.tpl");
		
		if(is_array($this->modules)){
			
			foreach ($this->modules as $key => $value){
				
				$out .= strtr( ($key == $this->current)? $tpl_selected : $tpl, 
					array(
						"{MODULE}" => $key,
						"{NAME}" => $value['modName'] 
					)
				);
			}
		}

		return $out;
	}
	
	public function setCurrent($current){
		$this->current = $current;
	}
}

?>